﻿
using System;
using System.Collections.Generic;
using System.Data;
using QLKS.Entity;

namespace QLKS.DAL
{
    public class HoaDonDAO
    {
        private SqlDataProvider dataProvider;

        public HoaDonDAO()
        {
            dataProvider = new SqlDataProvider();
        }

        public List<HoaDon> GetByAll(HoaDon obj)
        {
            List<HoaDon> lstData = new List<HoaDon>(); ;
            try
            {
                DataTable dt = dataProvider.ExecuteDataTable("sp_HoaDon_GetByAll"
                    , new string[] { "MaHoaDon", "NgayLapHD", "KhachHang", "TongTien", "GhiChu" }
                    , new object[] { obj.MaHoaDon, obj.NgayLapHD, obj.KhachHang, obj.TongTien, obj.GhiChu }
                );
                HoaDon entity;
                foreach(DataRow myrow in dt.Rows)
                {
                    entity = new HoaDon();
                    entity.MaHoaDon = Convert.ToInt32(myrow["MaHoaDon"]);
                    entity.NgayLapHD = myrow["NgayLapHD"] is DBNull ? string.Empty : myrow["NgayLapHD"].ToString();
                    entity.KhachHang = Convert.ToInt32(myrow["KhachHang"]);
                    entity.TongTien = Convert.ToDouble(myrow["TongTien"]);
                    entity.GhiChu = myrow["GhiChu"] is DBNull ? string.Empty : myrow["GhiChu"].ToString();
                    lstData.Add(entity);
                }
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lstData;
        }

        public bool Insert(HoaDon obj)
        {
            int i = dataProvider.ExecuteNonQuery("sp_HoaDon_Insert"
                    , new string[] { "MaHoaDon", "NgayLapHD", "KhachHang", "TongTien", "GhiChu" }
                    , new object[] { obj.MaHoaDon, obj.NgayLapHD, obj.KhachHang, obj.TongTien, obj.GhiChu }
                );
            return i == 1 ? true : false;
        }

        public bool Update(HoaDon obj)
        {
            int i = dataProvider.ExecuteNonQuery("sp_HoaDon_Update"
                    , new string[] { "MaHoaDon", "NgayLapHD", "KhachHang", "TongTien", "GhiChu" }
                    , new object[] { obj.MaHoaDon, obj.NgayLapHD, obj.KhachHang, obj.TongTien, obj.GhiChu }
                );
            return i == 1 ? true : false;
        }

        public bool Delete(HoaDon obj)
        {
            int i = dataProvider.ExecuteNonQuery("sp_HoaDon_Delete"
                    , new string[] { "MaHoaDon", "NgayLapHD", "KhachHang", "TongTien", "GhiChu" }
                    , new object[] { obj.MaHoaDon, obj.NgayLapHD, obj.KhachHang, obj.TongTien, obj.GhiChu }
                );
            return i == 1 ? true : false;
        }
    }
}
